package com.platform.hjt.erp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.UserType;
import com.platform.hjt.erp.model.ErpStock;
import com.platform.hjt.erp.model.ErpStorage;
import com.platform.hjt.erp.service.ErpStockService;
import com.platform.hjt.erp.service.ErpStorageService;

@Controller
@RequestMapping(value="/erpStorage")
public class ErpStorageController extends BaseController{

	@Resource
	private ErpStorageService erpStorageService;
	@Resource
	private ErpStockService erpStockService;
	
	@RequestMapping(value="/erpStorage_list.html")
	public String erpStorageListPage(){
		return "page/admin/erpStorage/erpStorage_list";
	}
	
	@RequestMapping(value="/erpStorage_list.do")
	@ResponseBody
	public Page<ErpStorage> erpStorageListDo(Page<ErpStorage>page){
		List<PropertyFilter>  propertyFilterList = PropertyFilter.buildFromHttpRequest(getRequest());
		propertyFilterList.add(new PropertyFilter("EQB_isDel", "false"));
		//非管理员人只能获取本公司的人员
		if(!getSessionUser().getUserType().equals(UserType.MASTER.getKey())){
			propertyFilterList.add(new PropertyFilter("EQS_sysCompanyInfo.sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		}
		return erpStorageService.getErpStorageList(page, propertyFilterList);
	}
	
	@RequestMapping(value="/erpStorage_edit.html")
	public String erpStorageEditPage(HttpServletRequest request, Model model, String id){
		if(StringUtils.isNotBlank(id)){
			model.addAttribute("erpStorage", erpStorageService.getErpStorage("erpStorageId", id));
		}
		
		return "page/admin/erpStorage/erpStorage_edit";
	}
	
	@RequestMapping(value="/erpStorage_detail_{id}.html")
	public String erpStorageDetailPage(Model model, @PathVariable String id){
		model.addAttribute("erpStorage", erpStorageService.getErpStorage("erpStorageId", id));
		return "page/admin/erpStorage/erpStorage_detail";
	}
	
	@RequestMapping(value="/erpStorage_edit.do")
	@ResponseBody
	public ReturnBody erpStorageEditDo(@Valid @ModelAttribute("erpStorage") ErpStorage erpStorage, BindingResult result){
		if(result.hasErrors()){
			return new ReturnBody(false, result.getAllErrors().get(0).getDefaultMessage());
		}
		
		if(erpStorage.getSysUserInfo() == null || StringUtils.isBlank(erpStorage.getSysUserInfo().getSysUserInfoId())){
			return new ReturnBody(false, "请选择仓库管理员");
		}
		
		return erpStorageService.saveErpStorage(erpStorage);
	}
	
	@RequestMapping(value="/erpStorage_del_{id}.do")
	@ResponseBody
	public ReturnBody erpStorageDelDo(@PathVariable String id){
		return erpStorageService.delErpStorage(new ErpStorage(id));
	}
	
	@RequestMapping(value="/erpStorage_select_list.do")
	@ResponseBody
	public JSONObject erpStorageSelectList(){
		List<PropertyFilter>  filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("EQB_isDel", "false"));
		filters.add(new PropertyFilter("EQS_sysCompanyInfo.sysCompanyInfoId", getSessionSysCompanyInfo().getSysCompanyInfoId()));
		
		JSONObject json = new JSONObject();
		List<ErpStorage>list = erpStorageService.getErpStorageList(filters);
		JSONArray arr = new JSONArray();
		for(ErpStorage es : list) {
			JSONObject temp = new JSONObject();
			temp.put("key", es.getStorageName());
			temp.put("value", es.getErpStorageId());
			arr.add(temp);
		}
		json.put("list", arr);
		return json;
	}
	
	/**
	 * 获取仓库的库存
	 * @Description 
	 * @author huangjt
	 * @param storageId
	 * @return
	 */
	@RequestMapping(value="/erpStorage_stock.do")
	@ResponseBody
	public List<ErpStock> getStorageStock(String storageId){
		List<PropertyFilter>  propertyFilterList = new ArrayList<PropertyFilter>();
		propertyFilterList.add(new PropertyFilter("EQS_erpStorage.erpStorageId", storageId));
		return erpStockService.getErpStockList(propertyFilterList);
	}
	
	
}
